package com.shelpe.services.sinbad.controller.sinbad;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.shelpe.services.sinbad.controller.actionLog.ActionLogConverter;
import com.shelpe.services.sinbad.controller.actionLog.ActionLogDto;
import com.shelpe.services.sinbad.entity.jpa.Adgroup;
import com.shelpe.services.sinbad.entity.jpa.AdgroupLog;
import com.shelpe.services.sinbad.entity.jpa.AreaList;
import com.shelpe.services.sinbad.entity.jpa.Campaign;
import com.shelpe.services.sinbad.entity.jpa.CampaignLog;
import com.shelpe.services.sinbad.entity.jpa.Creative;
import com.shelpe.services.sinbad.entity.jpa.CreativeLog;
import com.shelpe.services.sinbad.entity.jpa.DriveType;
import com.shelpe.services.sinbad.entity.jpa.KeywordsLog;
import com.shelpe.services.sinbad.repository.jpa.AdgroupRepository;
import com.shelpe.services.sinbad.repository.jpa.AreaListRepository;
import com.shelpe.services.sinbad.repository.jpa.CampaignRepository;
import com.shelpe.services.sinbad.repository.jpa.CreativeRepository;
import com.shelpe.services.sinbad.utils.DateUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/sinbadservice/v2")
@Api("v2版操作记录相关的API")
public class ActionLogController {
	public final static Predicate[] TEMPLATE = new Predicate[0];
	@Autowired
	private CampaignRepository campaignRepository;
	@Autowired
	private AdgroupRepository adgroupRepository;
	@Autowired
	private CreativeRepository creativeRepository;
	@Autowired
	private List<ActionLogConverter> converters;
	@Autowired
	private AreaListRepository areaListRepository;
	@Autowired
	private EntityManager em;

	@ApiOperation("计划操作记录api")
	@RequestMapping(path = "/users/{user_id}/campaigns/logs", method = RequestMethod.GET)
	public Page<ActionLogDto> findCampaignLogs(@PathVariable("user_id") final long userID,
			@RequestParam(name = "number", defaultValue = "0", required = false) int page,
			@RequestParam(name = "page_size", defaultValue = "20", required = false) int size,
			@RequestParam(name = "campaign_id", required = false, defaultValue = "0") final long campaignID,
			@RequestParam(name = "start", required = false) final String start,
			@RequestParam(name = "end", required = false) final String end) {
		final Date endDate = new Date();
		final Date startDate = DateUtils.addDays(endDate, -7);

		// CriteriaBuilder,用来构建CritiaQuery的构建器对象
		CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
		// CriteriaQuery,它包含着查询语句的条件各个部分，比如：select 、from、where、group by、orderby等
		CriteriaQuery<CampaignLog> criteriaQuery = criteriaBuilder.createQuery(CampaignLog.class);
		// 查询根，用于获取查询实例的属性，通过CriteriaQuery的from方法获取
		Root<CampaignLog> rootFrom = criteriaQuery.from(CampaignLog.class);
		// 查询条件
		List<Predicate> predicates = new ArrayList<Predicate>();
		if (StringUtils.isNotBlank(start) && StringUtils.isNotBlank(end)) {
			predicates.add(criteriaBuilder.between(rootFrom.<Date>get("updatedTime"), DateUtil.parseDate2date(start),
					DateUtils.addDays(DateUtil.parseDate2date(end), +1)));
		} else {
			predicates.add(criteriaBuilder.between(rootFrom.<Date>get("updatedTime"), startDate, endDate));
		}
		if (campaignID > 0) {
			predicates.add(criteriaBuilder.equal(rootFrom.get("campaignID"), campaignID));
		} else {
			List<Campaign> campaigns = campaignRepository.findByUserID(userID);
			List<Long> campaignIDs = new ArrayList<Long>();
			if (campaigns != null) {
				for (Campaign campaign : campaigns) {
					if (campaignIDs.indexOf(campaign.getAutoID()) == -1) {
						campaignIDs.add(campaign.getAutoID());
					}
				}
			}
			In<Long> ins = criteriaBuilder.in(rootFrom.<Long>get("campaignID"));
			for (long camID : campaignIDs) {
				ins.value(camID);
			}
			predicates.add(ins);
		}
		// 格式化参数
		criteriaQuery.where(criteriaBuilder.and(predicates.toArray(TEMPLATE)));
		// 默认按照id排序（从小到大）
		List<Order> orders = new ArrayList<Order>();
		orders.add(criteriaBuilder.desc(rootFrom.get("updatedTime")));
		criteriaQuery.orderBy(orders);
		// SQL查询对象
		TypedQuery<CampaignLog> createQuery = em.createQuery(criteriaQuery);
		// 分页参数
		Integer pageSize = size;
		Integer pageNo = page;
		// 计数查询结果条数
		TypedQuery<CampaignLog> createCountQuery = em.createQuery(criteriaQuery);
		// 实际查询返回分页对象
		createQuery.setFirstResult(pageNo * pageSize);
		createQuery.setMaxResults(pageSize);

		List<ActionLogDto> dtos = new ArrayList<ActionLogDto>();
		if (createQuery.getResultList() != null && createQuery.getResultList().size() > 0) {
			List<Long> campaignIDs = new ArrayList<Long>();
			for (CampaignLog po : createQuery.getResultList()) {
				if (campaignIDs.indexOf(po.getCampaignID()) == -1) {
					campaignIDs.add(po.getCampaignID());
				}
			}
			Map<Long, Campaign> campaignMap = new HashMap<Long, Campaign>();
			this.from(campaignIDs, null, null, campaignMap, null, null);

			List<AreaList> areas = (List<AreaList>) this.areaListRepository.findAll();

			if (createQuery.getResultList() != null && createQuery.getResultList().size() > 0) {
				for (CampaignLog po : createQuery.getResultList()) {
					ActionLogDto dto = this.covertCampaign(po, campaignMap, areas);
					if (dto != null) {
						dtos.add(dto);
					}
				}
			}
			return new PageImpl<ActionLogDto>(dtos, new PageRequest(page, size),
					createCountQuery.getResultList().size());
		}
		return new PageImpl<ActionLogDto>(dtos, new PageRequest(page, size), 0);
	}

	@ApiOperation("推广单元操作记录api")
	@RequestMapping(path = "/users/{user_id}/adgroups/logs", method = RequestMethod.GET)
	public Page<ActionLogDto> findAdgroupLogs(@PathVariable("user_id") final long userID,
			@RequestParam(name = "campaign_id", required = false, defaultValue = "0") final long campaignID,
			@RequestParam(name = "adgroup_id", required = false, defaultValue = "0") final long adgroupID,
			@RequestParam(name = "number", defaultValue = "0", required = false) int page,
			@RequestParam(name = "page_size", defaultValue = "20", required = false) int size,
			@RequestParam(name = "start", required = false) final String start,
			@RequestParam(name = "end", required = false) final String end) {
		final Date endDate = new Date();
		final Date startDate = DateUtils.addDays(endDate, -7);

		// CriteriaBuilder,用来构建CritiaQuery的构建器对象
		CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
		// CriteriaQuery,它包含着查询语句的条件各个部分，比如：select 、from、where、group by、orderby等
		CriteriaQuery<AdgroupLog> criteriaQuery = criteriaBuilder.createQuery(AdgroupLog.class);
		// 查询根，用于获取查询实例的属性，通过CriteriaQuery的from方法获取
		Root<AdgroupLog> rootFrom = criteriaQuery.from(AdgroupLog.class);
		// 查询条件
		List<Predicate> predicates = new ArrayList<Predicate>();
		if (StringUtils.isNotBlank(start) && StringUtils.isNotBlank(end)) {
			predicates.add(criteriaBuilder.between(rootFrom.<Date>get("updatedTime"), DateUtil.parseDate2date(start),
					DateUtils.addDays(DateUtil.parseDate2date(end), +1)));
		} else {
			predicates.add(criteriaBuilder.between(rootFrom.<Date>get("updatedTime"), startDate, endDate));
		}
		if (campaignID > 0) {
			predicates.add(criteriaBuilder.equal(rootFrom.get("campaignID"), campaignID));
		}
		if (adgroupID > 0) {
			predicates.add(criteriaBuilder.equal(rootFrom.get("adgroupID"), adgroupID));
		}
		if (adgroupID == 0 && campaignID == 0) {
			List<Campaign> campaigns = campaignRepository.findByUserID(userID);
			List<Long> campaignIDs = new ArrayList<Long>();
			if (campaigns != null) {
				for (Campaign campaign : campaigns) {
					if (campaignIDs.indexOf(campaign.getAutoID()) == -1) {
						campaignIDs.add(campaign.getAutoID());
					}
				}
			}
			In<Long> ins = criteriaBuilder.in(rootFrom.<Long>get("campaignID"));
			for (long camID : campaignIDs) {
				ins.value(camID);
			}
			predicates.add(ins);
		}
		// 格式化参数
		criteriaQuery.where(criteriaBuilder.and(predicates.toArray(TEMPLATE)));
		// 默认按照id排序（从小到大）
		List<Order> orders = new ArrayList<Order>();
		orders.add(criteriaBuilder.desc(rootFrom.get("updatedTime")));
		criteriaQuery.orderBy(orders);
		// SQL查询对象
		TypedQuery<AdgroupLog> createQuery = em.createQuery(criteriaQuery);
		// 分页参数
		Integer pageSize = size;
		Integer pageNo = page;
		// 计数查询结果条数
		TypedQuery<AdgroupLog> createCountQuery = em.createQuery(criteriaQuery);
		// 实际查询返回分页对象
		createQuery.setFirstResult(pageNo * pageSize);
		createQuery.setMaxResults(pageSize);

		List<ActionLogDto> dtos = new ArrayList<ActionLogDto>();
		if (createQuery.getResultList() != null && createQuery.getResultList().size() > 0) {
			List<Long> campaignIDs = new ArrayList<Long>();
			List<Long> adgroupIDs = new ArrayList<Long>();
			for (AdgroupLog po : createQuery.getResultList()) {
				if (campaignIDs.indexOf(po.getCampaignID()) == -1) {
					campaignIDs.add(po.getCampaignID());
				}
				if (adgroupIDs.indexOf(po.getAdgroupID()) == -1) {
					adgroupIDs.add(po.getAdgroupID());
				}
			}
			Map<Long, Campaign> campaignMap = new HashMap<Long, Campaign>();
			Map<Long, Adgroup> adgroupMap = new HashMap<Long, Adgroup>();
			this.from(campaignIDs, adgroupIDs, null, campaignMap, adgroupMap, null);

			if (createQuery.getResultList() != null && createQuery.getResultList().size() > 0) {
				for (AdgroupLog po : createQuery.getResultList()) {
					ActionLogDto dto = this.covertAdgroup(po, campaignMap, adgroupMap);
					if (dto != null) {
						dtos.add(dto);
					}
				}
			}
			return new PageImpl<ActionLogDto>(dtos, new PageRequest(page, size),
					createCountQuery.getResultList().size());
		}
		return new PageImpl<ActionLogDto>(dtos, new PageRequest(page, size), 0);
	}

	@ApiOperation("创意操作记录api")
	@RequestMapping(path = "/users/{user_id}/creatives/logs", method = RequestMethod.GET)
	public Page<ActionLogDto> findCreativeLogs(@PathVariable("user_id") final long userID,
			@RequestParam(name = "campaign_id", required = false, defaultValue = "0") final long campaignID,
			@RequestParam(name = "adgroup_id", required = false, defaultValue = "0") final long adgroupID,
			@RequestParam(name = "number", defaultValue = "0", required = false) int page,
			@RequestParam(name = "page_size", defaultValue = "20", required = false) int size,
			@RequestParam(name = "start", required = false) final String start,
			@RequestParam(name = "end", required = false) final String end) {
		final Date endDate = new Date();
		final Date startDate = DateUtils.addDays(endDate, -7);

		// CriteriaBuilder,用来构建CritiaQuery的构建器对象
		CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
		// CriteriaQuery,它包含着查询语句的条件各个部分，比如：select 、from、where、group by、orderby等
		CriteriaQuery<CreativeLog> criteriaQuery = criteriaBuilder.createQuery(CreativeLog.class);
		// 查询根，用于获取查询实例的属性，通过CriteriaQuery的from方法获取
		Root<CreativeLog> rootFrom = criteriaQuery.from(CreativeLog.class);
		// 查询条件
		List<Predicate> predicates = new ArrayList<Predicate>();
		if (StringUtils.isNotBlank(start) && StringUtils.isNotBlank(end)) {
			predicates.add(criteriaBuilder.between(rootFrom.<Date>get("updatedTime"), DateUtil.parseDate2date(start),
					DateUtils.addDays(DateUtil.parseDate2date(end), +1)));
		} else {
			predicates.add(criteriaBuilder.between(rootFrom.<Date>get("updatedTime"), startDate, endDate));
		}
		if (campaignID > 0) {
			List<Adgroup> adgroups = adgroupRepository.findByCampaignID(campaignID);
			List<Long> adgroupIDs = new ArrayList<Long>();
			if (adgroups != null && adgroups.size() > 0) {
				for (Adgroup adgroup : adgroups) {
					if (adgroupIDs.indexOf(adgroup.getAutoID()) == -1) {
						adgroupIDs.add(adgroup.getAutoID());
					}
				}
			}
			In<Long> ins = criteriaBuilder.in(rootFrom.<Long>get("adgroupID"));
			for (long adgID : adgroupIDs) {
				ins.value(adgID);
			}
			predicates.add(ins);
		}
		if (adgroupID > 0) {
			predicates.add(criteriaBuilder.equal(rootFrom.get("adgroupID"), adgroupID));
		}
		if (adgroupID == 0 && campaignID == 0) {
			List<Adgroup> adgroups = adgroupRepository.findByUserID(userID);
			List<Long> adgroupIDs = new ArrayList<Long>();
			if (adgroups != null && adgroups.size() > 0) {
				for (Adgroup adgroup : adgroups) {
					if (adgroupIDs.indexOf(adgroup.getAutoID()) == -1) {
						adgroupIDs.add(adgroup.getAutoID());
					}
				}
			}
			In<Long> ins = criteriaBuilder.in(rootFrom.<Long>get("adgroupID"));
			for (long adgID : adgroupIDs) {
				ins.value(adgID);
			}
			predicates.add(ins);
		}
		// 格式化参数
		criteriaQuery.where(criteriaBuilder.and(predicates.toArray(TEMPLATE)));
		// 默认按照id排序（从小到大）
		List<Order> orders = new ArrayList<Order>();
		orders.add(criteriaBuilder.desc(rootFrom.get("updatedTime")));
		criteriaQuery.orderBy(orders);
		// SQL查询对象
		TypedQuery<CreativeLog> createQuery = em.createQuery(criteriaQuery);
		// 分页参数
		Integer pageSize = size;
		Integer pageNo = page;
		// 计数查询结果条数
		TypedQuery<CreativeLog> createCountQuery = em.createQuery(criteriaQuery);
		// 实际查询返回分页对象
		createQuery.setFirstResult(pageNo * pageSize);
		createQuery.setMaxResults(pageSize);

		List<ActionLogDto> dtos = new ArrayList<ActionLogDto>();
		if (createQuery.getResultList() != null && createQuery.getResultList().size() > 0) {
			List<Long> adgroupIDs = new ArrayList<Long>();
			List<Long> campaignIDs = new ArrayList<Long>();
			for (CreativeLog po : createQuery.getResultList()) {
				if (adgroupIDs.indexOf(po.getAdgroupID()) == -1) {
					adgroupIDs.add(po.getAdgroupID());
				}
			}
			List<Adgroup> adgroups = this.adgroupRepository.findByAutoIDIn(adgroupIDs);
			if (adgroups != null && adgroups.size() > 0) {
				for (Adgroup adgroup : adgroups) {
					if (campaignIDs.indexOf(adgroup.getCampaignID()) == -1) {
						campaignIDs.add(adgroup.getCampaignID());
					}
				}
			}
			Map<Long, Campaign> campaignMap = new HashMap<Long, Campaign>();
			Map<Long, Adgroup> adgroupMap = new HashMap<Long, Adgroup>();
			this.from(campaignIDs, adgroupIDs, null, campaignMap, adgroupMap, null);

			if (createQuery.getResultList() != null && createQuery.getResultList().size() > 0) {
				for (CreativeLog po : createQuery.getResultList()) {
					ActionLogDto dto = this.covertCreative(po, adgroupMap, campaignMap);
					if (dto != null) {
						dtos.add(dto);
					}
				}
			}
			return new PageImpl<ActionLogDto>(dtos, new PageRequest(page, size),
					createCountQuery.getResultList().size());
		}
		return new PageImpl<ActionLogDto>(dtos, new PageRequest(page, size), 0);
	}

	@ApiOperation("关键词操作记录api")
	@RequestMapping(path = "/users/{user_id}/keywords/logs", method = RequestMethod.GET)
	public Page<ActionLogDto> findKeywordLogs(@PathVariable("user_id") final long userID,
			@RequestParam(name = "campaign_id", required = false, defaultValue = "0") final long campaignID,
			@RequestParam(name = "adgroup_id", required = false, defaultValue = "0") final long adgroupID,
			@RequestParam(name = "number", defaultValue = "0", required = false) int page,
			@RequestParam(name = "page_size", defaultValue = "20", required = false) int size,
			@RequestParam(name = "start", required = false) final String start,
			@RequestParam(name = "end", required = false) final String end) {
		final Date endDate = new Date();
		final Date startDate = DateUtils.addDays(endDate, -7);

		// CriteriaBuilder,用来构建CritiaQuery的构建器对象
		CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
		// CriteriaQuery,它包含着查询语句的条件各个部分，比如：select 、from、where、group by、orderby等
		CriteriaQuery<KeywordsLog> criteriaQuery = criteriaBuilder.createQuery(KeywordsLog.class);
		// 查询根，用于获取查询实例的属性，通过CriteriaQuery的from方法获取
		Root<KeywordsLog> rootFrom = criteriaQuery.from(KeywordsLog.class);
		// 查询条件
		List<Predicate> predicates = new ArrayList<Predicate>();
		if (StringUtils.isNotBlank(start) && StringUtils.isNotBlank(end)) {
			predicates.add(criteriaBuilder.between(rootFrom.<Date>get("updatedTime"), DateUtil.parseDate2date(start),
					DateUtils.addDays(DateUtil.parseDate2date(end), +1)));
		} else {
			predicates.add(criteriaBuilder.between(rootFrom.<Date>get("updatedTime"), startDate, endDate));
		}
		if (campaignID > 0) {
			List<Adgroup> adgroups = adgroupRepository.findByCampaignID(campaignID);
			List<Long> adgroupIDs = new ArrayList<Long>();
			if (adgroups != null && adgroups.size() > 0) {
				for (Adgroup adgroup : adgroups) {
					if (adgroupIDs.indexOf(adgroup.getAutoID()) == -1) {
						adgroupIDs.add(adgroup.getAutoID());
					}
				}
			}
			In<Long> ins = criteriaBuilder.in(rootFrom.<Long>get("adgroupID"));
			for (long adgID : adgroupIDs) {
				ins.value(adgID);
			}
			predicates.add(ins);
		}
		if (adgroupID > 0) {
			predicates.add(criteriaBuilder.equal(rootFrom.get("adgroupID"), adgroupID));
		}
		if (adgroupID == 0 && campaignID == 0) {
			List<Long> userIDs = new ArrayList<Long>();
			userIDs.add(userID);
			List<Long> adgroups = adgroupRepository.findAdgroupIDsByUserID(userIDs);
			List<DriveType> driveTypes = new ArrayList<DriveType>();
			driveTypes.add(DriveType.auto);
			driveTypes.add(DriveType.semiauto);
			List<Long> adgroupDriveTypeIDs = this.adgroupRepository.findAdgroupIDsAndDriveTypeByUserID(userIDs,
					driveTypes);
			List<Long> adgroupIDs = new ArrayList<Long>();
			if (adgroups != null && adgroups.size() > 0) {
				for (Long ID : adgroups) {
					if (adgroupIDs.indexOf(ID) == -1) {
						adgroupIDs.add(ID);
					}
				}
			}
			List<Long> needAdgroupIDs = new ArrayList<Long>();
			// 当推广组 低价and规则大于700个时 取700 否则查询直接gc
			if (adgroupDriveTypeIDs.size() > 0 && adgroupIDs.size() > 200) {
				needAdgroupIDs.addAll(adgroupIDs.subList(0, 150));
			} else {
				needAdgroupIDs.addAll(adgroups);
			}
			In<Long> ins = criteriaBuilder.in(rootFrom.<Long>get("adgroupID"));
			for (long adgID : needAdgroupIDs) {
				ins.value(adgID);
			}
			predicates.add(ins);
		}
		// 格式化参数
		criteriaQuery.where(criteriaBuilder.and(predicates.toArray(TEMPLATE)));
		// 默认按照id排序（从小到大）
		List<Order> orders = new ArrayList<Order>();
		orders.add(criteriaBuilder.desc(rootFrom.get("updatedTime")));
		criteriaQuery.orderBy(orders);
		// SQL查询对象
		TypedQuery<KeywordsLog> createQuery = em.createQuery(criteriaQuery);
		// 分页参数
		Integer pageSize = size;
		Integer pageNo = page;
		// 计数查询结果条数
		TypedQuery<KeywordsLog> createCountQuery = em.createQuery(criteriaQuery);
		// 实际查询返回分页对象
		createQuery.setFirstResult(pageNo * pageSize);
		createQuery.setMaxResults(pageSize);

		List<ActionLogDto> dtos = new ArrayList<ActionLogDto>();
		List<KeywordsLog> pos = null;
		int sum = 0;
		if (createQuery.getResultList() != null && createQuery.getResultList().size() > 0) {
			pos = createQuery.getResultList();
			sum = createCountQuery.getResultList().size();
		}

		if (pos != null && pos.size() > 0) {
			List<Long> adgroupIDs = new ArrayList<Long>();
			List<Long> campaignIDs = new ArrayList<Long>();
			for (KeywordsLog po : pos) {
				if (adgroupIDs.indexOf(po.getAdgroupID()) == -1) {
					adgroupIDs.add(po.getAdgroupID());
				}
			}
			List<Adgroup> adgroups = this.adgroupRepository.findByAutoIDIn(adgroupIDs);
			if (adgroups != null && adgroups.size() > 0) {
				for (Adgroup adgroup : adgroups) {
					if (campaignIDs.indexOf(adgroup.getCampaignID()) == -1) {
						campaignIDs.add(adgroup.getCampaignID());
					}
				}
			}
			Map<Long, Campaign> campaignMap = new HashMap<Long, Campaign>();
			Map<Long, Adgroup> adgroupMap = new HashMap<Long, Adgroup>();
			this.from(campaignIDs, adgroupIDs, null, campaignMap, adgroupMap, null);

			for (KeywordsLog po : pos) {
				ActionLogDto dto = this.covertKeyword(po, adgroupMap, campaignMap);
				if (dto != null) {
					dtos.add(dto);
				}
			}
			if (dtos == null || dtos.size() == 0) {
				return new PageImpl<ActionLogDto>(dtos, new PageRequest(page, size), 0);
			}
			return new PageImpl<ActionLogDto>(dtos, new PageRequest(page, size), sum);
		}
		return new PageImpl<ActionLogDto>(dtos, new PageRequest(page, size), 0);
	}

	/*** 映射类 ***/
	private ActionLogDto covertKeyword(KeywordsLog po, Map<Long, Adgroup> adgroupMap, Map<Long, Campaign> campaignMap) {
		ActionLogDto dto = null;
		if (this.converters != null) {
			for (ActionLogConverter converter : this.converters) {
				dto = converter.convertKeywrod(po, campaignMap, adgroupMap);
				if (dto != null) {
					break;
				}
			}
		}
		return dto;
	}

	private ActionLogDto covertCreative(CreativeLog po, Map<Long, Adgroup> adgroupMap,
			Map<Long, Campaign> campaignMap) {
		ActionLogDto dto = null;
		if (this.converters != null) {
			for (ActionLogConverter converter : this.converters) {
				dto = converter.convertCreative(po, campaignMap, adgroupMap);
				if (dto != null) {
					break;
				}
			}
		}
		return dto;
	}

	private ActionLogDto covertAdgroup(AdgroupLog po, Map<Long, Campaign> campaignMap, Map<Long, Adgroup> adgroupMap) {
		ActionLogDto dto = null;
		if (this.converters != null) {
			for (ActionLogConverter converter : this.converters) {
				dto = converter.convertAdgroup(po, campaignMap, adgroupMap);
				if (dto != null) {
					break;
				}
			}
		}
		return dto;
	}

	private ActionLogDto covertCampaign(CampaignLog po, Map<Long, Campaign> campaignMap, List<AreaList> areas) {
		ActionLogDto dto = null;
		if (this.converters != null) {
			for (ActionLogConverter converter : this.converters) {
				dto = converter.convertCampaign(po, campaignMap, areas);
				if (dto != null) {
					break;
				}
			}
		}
		return dto;
	}

	/*** 辅助类 ***/
	private void from(List<Long> campaignIDs, List<Long> adgroupIDs, List<Long> creativeIDs,
			Map<Long, Campaign> campaignMap, Map<Long, Adgroup> adgroupMap, Map<Long, Creative> creativeMap) {
		if (campaignIDs != null && campaignIDs.size() > 0) {
			List<Campaign> campaigns = this.campaignRepository.findByAutoIDIn(campaignIDs);
			if (campaigns != null && campaigns.size() > 0) {
				for (Campaign campaign : campaigns) {
					campaignMap.put(campaign.getAutoID(), campaign);
				}
			}
		}
		if (adgroupIDs != null && adgroupIDs.size() > 0) {
			List<Adgroup> adgroups = this.adgroupRepository.findByAutoIDIn(adgroupIDs);
			if (adgroups != null && adgroups.size() > 0) {
				for (Adgroup adgroup : adgroups) {
					adgroupMap.put(adgroup.getAutoID(), adgroup);
				}
			}
		}
		if (creativeIDs != null && creativeIDs.size() > 0) {
			List<Creative> creatives = this.creativeRepository.findByAutoIDIn(creativeIDs);
			if (creatives != null && creatives.size() > 0) {
				for (Creative creative : creatives) {
					creativeMap.put(creative.getAutoID(), creative);
				}
			}

		}
	}
}
